package com.js.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.js.mapper.CourseMapper;
import com.js.pojo.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@Controller
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseMapper courseMapper;
    /***
     * 分页
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryCourseByPage")
    public String queryCourseByPage(Model model,
                                  @RequestParam(required = true,defaultValue = "1") Integer page,
                                  @RequestParam(required = false,defaultValue = "4")Integer pageSize){
        //page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,pageSize);
        List<Course> courses = courseMapper.queryAllCourse();
        PageInfo<Course> coursePageInfo = new PageInfo<>(courses );
        model.addAttribute("page",coursePageInfo);
        return "course/courseList";
    }
    /***
     * 分页
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryCourseByPage1")
    public String queryCourseByPage1(Model model,
                                    @RequestParam(required = true,defaultValue = "1") Integer page,
                                    @RequestParam(required = false,defaultValue = "4")Integer pageSize){
        //page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录。
        PageHelper.startPage(page,pageSize);
        List<Course> courses = courseMapper.queryAllCourse();
        PageInfo<Course> coursePageInfo = new PageInfo<>(courses );
        model.addAttribute("page",coursePageInfo);
        return "course/courseListRead";
    }
    //跳转到添加课程的页面
    @RequestMapping("/toAdd")
    public String toAddCourse(){
        return "course/addCourse";
    }
    //添加课程的功能请求
    @RequestMapping("/add")
    public String addCourse(Course course){
        System.out.println("addCourse==>"+course.toString());
        System.out.println(course.getTeacher().getId());
        int i = courseMapper.addCourse(course);
        if (i>0){
            return "redirect:/course/queryCourseByPage";//添加课程后需要，重定向到查询全部课程
        }
        return "redirect:/course/toAdd";

    }
    @RequestMapping("/dele/{id}")
    public String deleteCourseById(@PathVariable("id") int id){
        int i = courseMapper.deleteCourseById(id);
        System.out.println(i);
        if (i>0){
            System.out.println("删除成功！！");
        }
        return "redirect:/course/queryCourseByPage";
    }
    @RequestMapping("/toUpdate")
    public String toUpdateCourse(int id, Model model){
        Course course = courseMapper.queryCourseById(id);
        model.addAttribute("QCourse",course);
        return "course/updateCourse";
    }
    //添加课程的功能请求
    @RequestMapping("/update")
    public String updateCourse(Course course, Model model){
        System.out.println("updateCourse==>"+course.toString());
        int i = courseMapper.updateCourse(course);
        if (i>0){
            return "redirect:/course/queryCourseByPage";//添加课程后需要，重定向到查询全部课程
        }
        return "redirect:/course/toUpdate";
    }
    @RequestMapping("/queryName")
    public String queryCourseByName(String courseName, Model model){
        System.out.println("要查询的课程:" + courseName);
        List<Course> courses = courseMapper.queryCourseBySubject(courseName);
        PageHelper.startPage(1,4);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        model.addAttribute("page",pageInfo);
        return "course/courseList";
    }
    @RequestMapping("/queryName1")
    public String queryCourseByName1(String courseName, Model model){
        System.out.println("要查询的课程:" + courseName);
        List<Course> courses = courseMapper.queryCourseBySubject(courseName);
        PageHelper.startPage(1,4);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        model.addAttribute("page",pageInfo);
        return "course/courseListRead";
    }
}
